const express=require('express')
const first=express.Router()
const axios=require('axios')

//分页查询大分类
first.get('/Classfy/queryForBidByPage',(req,res)=>{
    let {pageNow,pageSize}=req.query
    axios({
        method:'get',
        url:'/Admin/Classfy/queryForBidByPage',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            pageNow:pageNow||1,
            pageSize:pageSize||6,
            fatherId:-1
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})


//删除大分类
first.delete('/Classfy/delete',(req,res)=>{
    let {id}=req.query
    axios({
        method:'delete',
        url:'/Admin/Classfy/delete',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            id:id
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        // console.log(err);
        res.status(200).json({
            error:err
        })
    })
})


//添加大分类
first.post('/Classfy/add',(req,res)=>{
    let {classfyName}=req.query
    axios({
        method:'post',
        url:'/Admin/Classfy/add',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            classfyName:classfyName,
            fatherId:-1,
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//修改大分类
first.get('/Classfy/update',(req,res)=>{
    let {classfyName,id}=req.query
    axios({
        method:'put',
        url:'/Admin/Classfy/update',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            classfyName:classfyName,
            fatherId:-1,
            id:id
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//不分页查询大分类
first.get('/Classfy/queryForBidByPageS',(req,res)=>{
    axios({
        method:'get',
        url:'/Admin/Classfy/queryForBidByPage',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            pageNow:1,
            pageSize:6,
            fatherId:-1
        }
    }).then(value=>{
        console.log(value.data.data.total);
        axios({
            method:'get',
            url:'/Admin/Classfy/queryForBidByPage',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
                token:(req.session.token?req.session.token:'')
            },
            params:{
                pageNow:1,
                pageSize:value.data.data.total,
                fatherId:-1
            }
        }).then(data=>{
            res.status(200).json({
                key:data.data
            })
        }).catch(err=>{
            res.status(500).json({
                err:err
            })
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

module.exports=first